package handlers

import (
	"jianianhua-server/config"
	"jianianhua-server/services"
	"jianianhua-server/utils"
	"strconv"

	"github.com/gin-gonic/gin"
)

type BannerHandler struct {
	bannerService *services.BannerService
	roleService   *services.RoleService
	serverConfig  *config.ServerConfig
}

func NewBannerHandler() *BannerHandler {
	return &BannerHandler{
		bannerService: services.NewBannerService(),
		roleService:   services.NewRoleService(),
		serverConfig:  config.LoadServerConfig(),
	}
}

// CreateBanner 创建轮播图
func (h *BannerHandler) CreateBanner(c *gin.Context) {
	userID, exists := utils.GetUserIDFromContext(c)
	if !exists {
		utils.UnauthorizedResponse(c, "用户未登录")
		return
	}

	// 检查是否为管理员
	isAdmin, err := h.roleService.IsAdmin(userID)
	if err != nil || !isAdmin {
		utils.ForbiddenResponse(c, "需要管理员权限")
		return
	}

	var req services.CreateBannerRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		utils.BadRequestResponse(c, "参数错误: "+err.Error())
		return
	}

	banner, err := h.bannerService.CreateBanner(&req)
	if err != nil {
		utils.BadRequestResponse(c, err.Error())
		return
	}

	utils.SuccessResponse(c, banner)
}

// GetBanners 获取轮播图列表
func (h *BannerHandler) GetBanners(c *gin.Context) {
	banners, err := h.bannerService.GetBanners()
	if err != nil {
		utils.InternalServerErrorResponse(c, err.Error())
		return
	}

	// 将相对路径转换为完整URL
	for i := range banners {
		if banners[i].ImageURL != "" {
			banners[i].ImageURL = h.serverConfig.GetImageURL(c.Request.Host, banners[i].ImageURL)
		}
	}

	utils.SuccessResponse(c, banners)
}

// GetBanner 获取轮播图详情
func (h *BannerHandler) GetBanner(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.BadRequestResponse(c, "无效的ID")
		return
	}

	banner, err := h.bannerService.GetBanner(uint(id))
	if err != nil {
		utils.NotFoundResponse(c, err.Error())
		return
	}

	utils.SuccessResponse(c, banner)
}

// UpdateBanner 更新轮播图
func (h *BannerHandler) UpdateBanner(c *gin.Context) {
	userID, exists := utils.GetUserIDFromContext(c)
	if !exists {
		utils.UnauthorizedResponse(c, "用户未登录")
		return
	}

	// 检查是否为管理员
	isAdmin, err := h.roleService.IsAdmin(userID)
	if err != nil || !isAdmin {
		utils.ForbiddenResponse(c, "需要管理员权限")
		return
	}

	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.BadRequestResponse(c, "无效的ID")
		return
	}

	var req services.UpdateBannerRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		utils.BadRequestResponse(c, "参数错误: "+err.Error())
		return
	}

	banner, err := h.bannerService.UpdateBanner(uint(id), &req)
	if err != nil {
		utils.BadRequestResponse(c, err.Error())
		return
	}

	utils.SuccessResponse(c, banner)
}

// DeleteBanner 删除轮播图
func (h *BannerHandler) DeleteBanner(c *gin.Context) {
	userID, exists := utils.GetUserIDFromContext(c)
	if !exists {
		utils.UnauthorizedResponse(c, "用户未登录")
		return
	}

	// 检查是否为管理员
	isAdmin, err := h.roleService.IsAdmin(userID)
	if err != nil || !isAdmin {
		utils.ForbiddenResponse(c, "需要管理员权限")
		return
	}

	idStr := c.Param("id")
	id, err := strconv.ParseUint(idStr, 10, 32)
	if err != nil {
		utils.BadRequestResponse(c, "无效的ID")
		return
	}

	err = h.bannerService.DeleteBanner(uint(id))
	if err != nil {
		utils.BadRequestResponse(c, err.Error())
		return
	}

	utils.SuccessResponse(c, gin.H{"message": "删除成功"})
}
